package com.xyazm.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.xyazm.product.entity.BrandEntity;
import com.xyazm.product.entity.CategoryBrandRelationEntity;
import com.xyazm.product.entity.vo.BrandVo;
import com.xyazm.product.service.CategoryBrandRelationService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xyazm.common.utils.PageUtils;
import com.xyazm.common.utils.R;

/**
 * 品牌分类关联
 *
 * @author xuyou
 * @date 2020-06-11 17:15:45
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {

	@Autowired
	private CategoryBrandRelationService categoryBrandRelationService;

	
    /**
     *  /product/categorybrandrelation/brands/list
     *
     *  1、Controller：处理请求，接受和校验数据
     *  2、Service接受controller传来的数据，进行业务处理
     *  3、Controller接受Service处理完的数据，封装页面指定的vo
     */
    @GetMapping("/brands/list")
    public R relationBrandsList(@RequestParam(value = "catId",required = true)Long catId){
        List<BrandEntity> vos = categoryBrandRelationService.getBrandsByCatId(catId);

        List<BrandVo> collect = vos.stream().map(item -> {
            BrandVo brandVo = new BrandVo();
            brandVo.setBrandId(item.getBrandId());
            brandVo.setBrandName(item.getName());

            return brandVo;
        }).collect(Collectors.toList());

        return R.ok().put("data",collect);

    }
	
	/**
	 * 根据品牌id查询品牌和分类关联关系
	 * 
	 * @Title: catelogList
	 * @Description:
	 * @Author xuyou
	 * @DateTime 2020年6月25日 下午9:52:09
	 * @param brandId 品牌id
	 * @return
	 */
	@GetMapping("/catelog/list")
	public R catelogList(@RequestParam("brandId") Long brandId) {
		List<CategoryBrandRelationEntity> list = categoryBrandRelationService
				.list(new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id", brandId));
		return R.ok().put("data", list);
	}

	/**
	 * 列表
	 */
	@RequestMapping("/list")
	public R list(@RequestParam Map<String, Object> params) {

		PageUtils page = categoryBrandRelationService.queryPage(params);
		return R.ok().put("page", page);
	}

	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	public R info(@PathVariable("id") Long id) {

		CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);
		return R.ok().put("categoryBrandRelation", categoryBrandRelation);
	}

	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation) {

		categoryBrandRelationService.saveDetail(categoryBrandRelation);
		return R.ok();
	}

	


	
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation) {

		categoryBrandRelationService.updateById(categoryBrandRelation);
		return R.ok();
	}

	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	public R delete(@RequestBody Long[] ids) {

		categoryBrandRelationService.removeByIds(Arrays.asList(ids));
		return R.ok();
	}

}
